package 第5次作业;

public class StringArrayOperations {
    public static void main(String[] args) {
        // 问题1：字符串转double数组、排序、计算sin值
        String strNumbers = "60,120,30,90,180";

        // 将字符串分割并转换为double数组
        String[] strArray = strNumbers.split(",");
        double[] numArray = new double[strArray.length];
        for (int i = 0; i < strArray.length; i++) {
            numArray[i] = Double.parseDouble(strArray[i]);
        }

        // 使用Arrays类排序
        java.util.Arrays.sort(numArray);

        // 计算arr[0]度的sin值（注意：Math.sin需要弧度值）
        double angleInRadians = Math.toRadians(numArray[0]);
        double sinValue = Math.sin(angleInRadians);

        // 输出问题1的结果
        System.out.println("排序后的数组：");
        for (double num : numArray) {
            System.out.print(num + " ");
        }
        System.out.println("\n数组第一个元素(" + numArray[0] + "度)的sin值：" + sinValue);

        // 问题2：String转StringBuffer并替换字符串
        String originalStr = "A dog is a kind of animal";
        StringBuffer sb = new StringBuffer(originalStr);

        // 查找并替换dog为cat
        int index = sb.indexOf("dog");
        if (index != -1) {
            sb.replace(index, index + 3, "cat");
        }

        // 输出问题2的结果
        System.out.println("\n替换后的字符串：" + sb.toString());
    }
}